from flask import Blueprint, render_template, request

from config import db
from models import House, User, Recommend
from utils.pearson_recommend import recommend

detail_page = Blueprint('detail_page', __name__)


@detail_page.route('/house/<int:hid>')
def detail(hid):
    # 从数据库中查找房源ID为hid的房源对象
    house = House.query.get(hid)
    # 获取房源对象的配套设施信息
    facilities_str = house.facilities
    # 将配套设施以-作为分割保存在列表facilities_list中
    facilities_list = facilities_str.split('-')

    # 定义一个用来存放推荐房源的列表变量
    recommend_li = []

    # 判断用户是否处于登录状态下
    name = request.cookies.get('name')

    # 在登录状态下
    if name:
        # 获取用户对象
        user = User.query.filter(User.name == name).first()
        # 获取用户对象的浏览记录，格式为'123，234，345'或者null
        seen_id_str = user.seen_id
        # 存在浏览记录
        if seen_id_str:
            # 将浏览记录中保存的字符串转换成列表，比如'123，234，345'==>['123','234','345']
            seen_id_list = seen_id_str.split(',')
            # 借助set()函数去重
            set_id = set([int(i) for i in seen_id_list])
            # 判断hid是否在浏览记录中
            if hid not in set_id:
                new_seen_id_str = seen_id_str + ',' + str(hid)
                user.seen_id = new_seen_id_str
                db.session.commit()
        else:
            # 直接将当前的hid插入到浏览记录中
            user.seen_id = str(hid)
            db.session.commit()
        # 查询house_recommend表中是否有当前用户对此房源的浏览记录
        info = Recommend.query.filter(Recommend.user_id == user.id, Recommend.house_id == house.id).first()
        # 第一种情况，该用户已经浏览过此房源，就对推荐表中score进行+1操作
        if info:
            new_score = info.score + 1
            info.score = new_score
            db.session.commit()
        # 第二种情况，该用户没有浏览过此房源，直接插入一条新的数据
        else:
            new_info = Recommend(user_id=user.id, house_id=house.id, title=house.title, address=house.address,
                                 block=house.block, score=1)
            db.session.add(new_info)
            db.session.commit()
        result = recommend(user.id)
        # 有推荐房源，此时返回推荐房源给用户
        if result:
            for recommend_hid, recommend_num in result:
                recommend_house = House.query.get(int(recommend_hid))
                recommend_li.append(recommend_house)
        # 推荐房源列表为空，此时返回同小区的房源给用户
        else:
            ordinary_recommend = House.query.filter(House.address == house.address).order_by(
                House.page_views.desc()).all()

            if len(ordinary_recommend) > 6:
                recommend_li = ordinary_recommend[:6]
            else:
                recommend_li = ordinary_recommend
    # 未登陆状态下
    else:
        ordinary_recommend = House.query.filter(House.address == house.address).order_by(House.page_views.desc()).all()

        if len(ordinary_recommend) > 6:
            recommend_li = ordinary_recommend[:6]
        else:
            recommend_li = ordinary_recommend

    return render_template('detail_page.html', house=house, facilities=facilities_list, recommend_li=recommend_li)


# 处理交通有无的情况
def deal_none(word):
    if len(word) == 0 or word is None:
        return '暂无信息'
    else:
        return word


detail_page.add_app_template_filter(deal_none, 'dealNone')
